
function TreeEditRender(tree){
	this.tree = tree ;
	this.id = TreeEditRender.defaultInstanceName ;
	this.showChapterNo = true ;
	return this ;
}

TreeEditRender.defaultInstanceName = "TER";

TreeEditRender.prototype.bindById = function () {
    var key = "TreeEditRender." + this.id;
    window[key] = this;
}

function findTreeEditRenderInstance() {
    var key = "TreeEditRender." + TreeEditRender.defaultInstanceName;
    return window[key];
}


TreeEditRender.prototype.refresh = function(){
	this.tree.init();
	var html = this.getTreeHTML() ;
//	document.write(html);
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeEditRender.prototype.render = function(){
	if(!this.tree.neatened){
		this.tree.init();
	}
	var html = this.getTreeHTML() ;
	document.write("<div id='DivTree' style='width:100%;height:100%;text-align:left;vertical-align:top;'></div>");
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeEditRender.prototype.getTreeHTML = function(){
	var buffer = new StringBuffer();
	buffer.append("<table cellspacing=0 cellpadding=0>");

	for(var i=0; i<this.tree.nodes.size(); i++){
		var node = this.tree.nodes.get(i);
		buffer.append(this.getTreeNodeHTML(node));
	}
	
	buffer.append("</table>");
	return buffer.toString();
}

TreeEditRender.prototype.getTreeNodeHTML = function(node){
	var buffer = new StringBuffer();

	buffer.append("<tr>");
	buffer.append("<td onclick=\"showDropMenu('").append(node.id).append("',event)\">");
	
	buffer.append("<table cellspacing=0 cellpadding=0>");
	buffer.append("<tr>");
	var lines = node.lines ;
	for(var i=0;i<lines.length;i++){
		if(lines[i] == "1"){
			buffer.append("<td><div class='YHMenu_nodeLine'></div></td>");
		}else{
			buffer.append("<td><div class='YHMenu_nodeEmpty'></div></td>");
		}
	}
	buffer.append("<td class='").append(this.getNodeCss(node)).append("'><div style='width:16px;'></div></td>");
	buffer.append("<td><div class='YHMenu_nodeIcon'><img src='").append(node.icon).append("' class='menuIcon'></img></div></td>");
	
	
	// buffer.append("<td class='YHMenu_nodeName'><span class='spanNode1' onmouseover=\"this.className='spanNode2';\" onmouseout=\"this.className='spanNode1';\">").append(node.name).append("</span></td>");
	buffer.append("<td class='YHMenu_nodeName'><span class='spanNode1' onmouseover=\"this.className='spanNode2';\" onmouseout=\"this.className='spanNode1';\" onclick=\"nodeClicked('").append(node.id).append("')\">");

	if(this.showChapterNo){
		buffer.append(node.chapterNo);
		// buffer.append('-').append(node.sequence);
		buffer.append(" ");
	}

	buffer.append(node.name);

	buffer.append("</tr>");
	buffer.append("</table>");

	buffer.append("</td>");
	buffer.append("</tr>");
	
	return buffer.toString();
}

TreeEditRender.prototype.getNodeCss = function(node){
	var openStatus = '1';
	//var str = this.openStatus + this.youngeBrotherStatus + this.sonStatus + "YHMenu_node" ;
	var str = "YHMenu_node" + openStatus + (node.hasYoungerBrother()?"1":"0") + (node.hasChild()?"1":"0");
	return str ;
}

function showDropMenu(id,event){
	var editRender = findTreeEditRenderInstance();
	var node = editRender.tree.findNodeById(id);
	var menuTree = new Tree("m00","MENU","root.gif");
	menuTree.addNode(new TreeNode('m01',NODE_ADD_CHILD,null,'',"treeNodeAddNode('"+id+"')",true));
	menuTree.addNode(new TreeNode('m07',NODE_CHANGE_NAME,null,'',"treeNodeEditNode('"+id+"')",true));
	menuTree.addNode(new TreeNode('m02',NODE_MOVE_UP,null,'../tree/moveUp.gif',"treeNodeMoveUp('"+id+"')",node.canMoveUp()));
	menuTree.addNode(new TreeNode('m03',NODE_MOVE_DOWN,null,'../tree/moveDown.gif',"treeNodeMoveDown('"+id+"')",node.canMoveDown()));
	menuTree.addNode(new TreeNode('m04',NODE_MOVE_LEFT,null,'../tree/moveLeft.gif',"treeNodeMoveLeft('"+id+"')",node.canMoveLeft()));
	menuTree.addNode(new TreeNode('m05',NODE_MOVE_RIGHT,null,'../tree/moveRight.gif',"treeNodeMoveRight('"+id+"')",node.canMoveRight()));
	menuTree.addNode(new TreeNode('m06',NODE_DELETE,null,'../tree/deleteNode.gif',"treeNodeDelete('"+id+"')",true));
	// menuTree.addNode(new TreeNode('m07','Move To',null,'../tree/node.gif','',true));
	
	/*
	var size = editRender.tree.nodes.size() ;
	for(var i=0;i<size;i++){
		var menu = editRender.tree.nodes.get(i);
		var txt = '' ;
		for(var j=0;j<menu.rank;j++){
			txt += "����";
		}
		menuTree.addNode(new TreeNode(menu.id,txt+menu.name,'m07',menu.icon,'',menu.id != id && !node.isAncestorOf(menu)));
		if(node.parentId != null && node.parentId == menu.id){
			continue ;
		}
		menuTree.addNode(new TreeNode(menu.id+'02','�ӽڵ���',menu.id,'',"moveUnder('"+id+"','"+menu.id+"')",true));
	}
	*/

	menuTree.init();
	var menuRender = new RightMenuRender(menuTree);
	menuRender.init();
	menuRender.bindById();
	menuRender.show(menuTree.root,event);
}


function randomInt(low, high) { 
	return Math.floor(Math.random()*(high-low+1)) + low; 
}

function getUniqueId(){
	var t = new Date();
	var s = t.getTime()+"" ;
	var l = s.length;
	var b = new StringBuffer();
	b.append(s);
	// alert(l) ;
	var len = 16 ;
	for(var i=0;i<(len-l);i++){
		b.append(randomInt(0,9));
	}
	return b.toString();
}

// alert(getUniqueId());

var newIndex = 0 ;
function treeNodeAddNode(id){
	var editRender = findTreeEditRenderInstance();
	var name = prompt(PLEASE_ENTER_NODE_NAME,'');
	if(name == ""){
		return ;
	}

	var newNodeId = getUniqueId();
	var newNode = new TreeNode(newNodeId,name,null,'../tree/node.gif','',true);
	newNode.parentId = id ;
	editRender.tree.addNode(newNode);
	editRender.refresh();
}

function treeNodeEditNode(id){
	var editRender = findTreeEditRenderInstance();
	var name = prompt(PLEASE_ENTER_NODE_NAME,'');
	if(name == ""){
		return ;
	}

	var node = editRender.tree.findNodeById(id);
	node.name = name ;
	editRender.refresh();
}

function treeNodeDelete(id){
	var editRender = findTreeEditRenderInstance();
	var node = editRender.tree.findNodeById(id);
	node.parentNode.removeChild(node);
	editRender.refresh();
}

function treeNodeMoveUp(id){
	var editRender = findTreeEditRenderInstance();
	var node = editRender.tree.findNodeById(id);
	node.moveUp();
	editRender.refresh();
}
function treeNodeMoveDown(id){
	var editRender = findTreeEditRenderInstance();
	var node = editRender.tree.findNodeById(id);
	node.moveDown();
	editRender.refresh();
}
function treeNodeMoveLeft(id){
	var editRender = findTreeEditRenderInstance();
	var node = editRender.tree.findNodeById(id);
	node.moveLeft();
	editRender.refresh();
}
function treeNodeMoveRight(id){
	var editRender = findTreeEditRenderInstance();
	var node = editRender.tree.findNodeById(id);
	node.moveRight();
	editRender.refresh();
}
function moveUnder(nodeId,toId){
//	alert(nodeId+","+toId);
	var editRender = findTreeEditRenderInstance();
	editRender.tree.moveNodeUnder(nodeId,toId);
	editRender.refresh();
}